<?php

namespace App\Http\Controllers\Income;

use App\Http\Controllers\Controller;
use App\Imports\Income\IncomeCharteredCarImport;
use App\Models\Income\IncomeCharteredCar;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class IncomeCharteredCarController extends Controller
{
    public function incomeCharteredCarList(Request $request)
    {
        $params = $countParams = $request->all();
        unset($countParams['pageNumber'], $countParams['pageSize']);

        $data = $this->getIncomeCharteredCarList($params)
            ->orderByDesc('created_at')
            ->get()
            ->toArray();
        $dataCount = $this->getIncomeCharteredCarList($countParams)->count();

        return response()->formatJson(['charteredCarRecord' => $data, 'count' => $dataCount]);
    }

    public function getIncomeCharteredCarList($params): \Illuminate\Database\Eloquent\Builder
    {
        $query = IncomeCharteredCar::query();

        if (isset($params['license_plate_num'])) {
            $query->where('license_plate_num', 'like', '%'.$params['license_plate_num'].'%');
        }

        if (isset($params['pageNumber'], $params['pageSize'])) {
            $query
                ->limit($params['pageSize'])
                ->offset(($params['pageNumber'] - 1) * $params['pageSize']);
        }

        return $query;
    }

    /**
     * 创建车辆信息
     *
     * @param  Request  $request
     *
     * @return mixed
     */
    public function storeCharteredCar(Request $request)
    {
        $params = $request->all();
        if ($params['license_plate_num']) {
            $params['license_plate_num'] = Str::of($params['license_plate_num'])->replace('陕G', '');
        }
        $charteredCar = new IncomeCharteredCar($params);
        $result = $charteredCar->save();
        $message = $result ? '添加成功' : '添加失败';

        return response()->formatJson($result, $message);
    }

    /**
     * 修改文章
     *
     * @param  Request  $request
     * @param           $id
     *
     * @return mixed
     */
    public function updateCharteredCar(Request $request, $id)
    {
        $params = $request->all();
        if ($params['license_plate_num']) {
            $params['license_plate_num'] = Str::of($params['license_plate_num'])->replace('陕G', '');
        }
        $charteredCar = IncomeCharteredCar::query()->find($id);

        $result = $charteredCar->update($params);

        $message = $result ? '修改成功' : '修改失败';

        return response()->formatJson($result, $message);
    }

    /**
     * 删除车辆信息
     *
     * @param  Request  $request
     *
     * @return mixed
     */
    public function destroyCharteredCar(Request $request)
    {
        $result = false;
        if ($request->has('ids')) {
            $ids = explode(',', $request['ids']);
            $charteredCarCount = IncomeCharteredCar::destroy($ids);
            $result = $charteredCarCount !== 0;
        }

        $message = $result ? '删除成功' : '删除失败';

        return response()->formatJson($result, $message);
    }
}
